#!/usr/bin/env bash
# Copyright 2017 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -eu

# Generates a docker run command to run a automatic container release config
# validator released as a Docker image. Argument list:
# docker_path- Path to the docker executable.
# spec_file_path- Absolute path to the YAML spec file to validate.
# spec_file_container_path- The path to copy the spec file to in the docker
#                           container.
# image_name- The docker image for the checker that will be run.
# cmd_args- Arguments to pass to the checker.

function guess_runfiles() {
    if [ -d ${BASH_SOURCE[0]}.runfiles ]; then
        # Runfiles are adjacent to the current script.
        echo "$( cd ${BASH_SOURCE[0]}.runfiles && pwd )"
    else
        # The current script is within some other script's runfiles.
        mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
        echo $mydir | sed -e 's|\(.*\.runfiles\)/.*|\1|'
    fi
}

RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}"

# Resolve the docker tool path.
DOCKER="%{docker_path}"
DOCKER_FLAGS="%{docker_flags}"

if [[ -z "$DOCKER" ]]; then
    echo >&2 "error: docker not found; do you need to manually configure the docker toolchain?"
    exit 1
fi

# Create a new docker container that will run the checker.
container_id=$("$DOCKER" $DOCKER_FLAGS create %{image_name} %{cmd_args})

specs=(%{specs})
spec_container_paths=(%{spec_container_paths})

# Copy the spec files to the container. The "-L" is to follow symlinks.
if [ ${#specs[@]} -ne ${#spec_container_paths[@]} ]; then
    echo "Given list of spec files had length ${#specs[@]} which didn't match "\
    "the ${#spec_container_paths[@]} length of the list of corresponding "\
    "container paths"
    exit 1
fi
for ((i=0;i<${#specs[@]};i++)); do
    "$DOCKER" $DOCKER_FLAGS cp -L ${specs[$i]} $container_id:${spec_container_paths[$i]}
done

# Start the container that will run the checker logic.
"$DOCKER" $DOCKER_FLAGS start $container_id

# Wait for the checker to finish running.
retcode=$("$DOCKER" $DOCKER_FLAGS wait $container_id)

# Print all logs generated by the container.
"$DOCKER" $DOCKER_FLAGS logs $container_id

# Delete the container.
"$DOCKER" $DOCKER_FLAGS rm $container_id

exit $retcode

